package com.cefa.edu.dao;

import com.cefa.edu.model.StudentInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.security.access.method.P;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

@Mapper
public interface StudentStageDataMapper {

    /**
     * @aa 根据学生id,学习阶段id,学校id 判断该学生的学校教育阶段数据是否已经入库
     * @aa add by chaijian 2018-08-14
     */
    @Select({"select count(ss.id) from cefa_edu_student_stage_data ss where user_id=#{userId} and ss.education_stage_id=#{stageId} and ss.school_id=#{schoolId}"})
    int getCountFormStudentStageDataTable(@Param("userId")Integer userId, @Param("stageId")Integer stageId,@Param("schoolId")Integer schoolId);

    /**
     * @aa 添加学生教育阶段数据
     * @aa add by chaijian 2018-08-14
     */
    @Insert({"insert into cefa_edu_student_stage_data(user_id,class_name,grade_name,education_stage_name,start_year,school_id,education_stage_id,level,stage_type,grade_id)" +
            "values(#{userId},#{className},#{gradeName},#{phaseName},#{startYear},#{schoolId},#{stageId},#{level},#{stageType},#{gradeId})"})
    int saveStudentStageData(Map<String,Object> paramsMap);

    @Select("select\n" +
            "a.*,b.name from \n" +
            "(select * from cefa_edu_student_stage_data \n" +
            "where user_id in (select DISTINCT(user_id) \n" +
            "from cefa_edu_user_education_level where level_id in #(levels))) a\n" +
            "left join cefa_edu_user b on a.user_id=b.id group by a.user_id")
    List<StudentInfo> findStudentByLevelId(@PathParam("levels") String levels);


    /**
     * @aa 获取当前系统最早毕业生毕业年份
     * @aa add by chaijian 2018-08-16
     */
    @Select({"select min(s.end_year) from cefa_edu_student_stage_data s where s.graduate=1 ORDER BY s.start_year asc "})
    Integer getMinGraduatedYear();

    /**
     * @aa 获取当前系统最早毕业生id
     * @aa add by chaijian 2018-08-28
     */
    @Select({"select DISTINCT s.user_id userId from cefa_edu_student_stage_data s where s.graduate=1"})
    List<Integer> getGraduateIds();
}
